package com.caijg.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caijg.exception.CustomException;
import com.caijg.mapper.ModelMapper;
import com.caijg.model.entity.Model;
import com.caijg.service.IModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.caijg.constant.RedisKeyConstant.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author caijg
 * @since 2025-05-07
 */
@Service
@Slf4j
public class ModelServiceImpl extends ServiceImpl<ModelMapper, Model> implements IModelService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Model getCurrentSimpleChatModel() {
        return this.getModelByName(CHAT_MODEL_KEY);
    }

    @Override
    public Model getCurrentEvaluateModel() {
        return this.getModelByName(EVALUATE_MODEL_KEY);
    }

    @Override
    public Model getcurrentTongueModel() {
        return this.getModelByName(TONGUE_MODEL_KEY);
    }

    private Model getModelByName(String modelName) {
        // 从redis中读取模型信息对象
        String jsonStr = redisTemplate.opsForValue().get(modelName);
        if (StrUtil.isBlank(jsonStr)) {
            throw new CustomException("当前模型不存在", 500);
        }
        // 将json字符串转成模型对象
        return JSONUtil.toBean(jsonStr, Model.class);
    }
}